package a01_数组;

/**
 * <p>
 * a05_长度最小的子数组复习6
 * </p>
 *
 * @author flyduck
 * @since 2025/4/8
 */
public class a05_长度最小的子数组复习6 {
    public static void main(String[] args) {
        System.out.println(minSubArrayLen(7, new int[]{2, 3, 1, 2, 4, 3}));
    }
    public static int minSubArrayLen(int target, int[] nums) {
        int leftIdx = 0;
        int sum = 0;
        int minLength = Integer.MAX_VALUE;
        for (int rightIdx = 0; rightIdx < nums.length; rightIdx++) {
            sum += nums[rightIdx];
            while (sum >= target && leftIdx <= rightIdx){//移动leftIdx
                minLength = Math.min(minLength, rightIdx - leftIdx + 1);
                sum -= nums[leftIdx];
                leftIdx++;
            }
        }
        return minLength != Integer.MAX_VALUE ? minLength : 0;
    }
}
